<template>
  <div class="system-dic-container layout-padding">
    <el-card class="layout-padding-auto" shadow="hover">
      <el-form ref="queryFormRef" :model="state.tableData.param.dataObj" inline>
        <el-form-item class="!mr-4" prop="dictType">
          <el-input v-model="state.tableData.param.dataObj.dictType"
                    placeholder="请输入字典类型" size="default"
                    style="max-width: 180px"/>
        </el-form-item>
        <el-form-item class="!mr-4" prop="dictName">
          <el-input v-model="state.tableData.param.dataObj.dictName"
                    placeholder="请输入字典名称" size="default"
                    style="max-width: 180px"/>
        </el-form-item>
        <el-form-item v-auth="'dict.list'" class="!mr-4">
          <el-button class="ml10" size="default" type="primary" @click="getTableData">
            <el-icon>
              <ele-Search/>
            </el-icon>
            查询
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'dict.list'" class="!mr-4">
          <el-button size="default" type="primary" @click="onHandleResetFields">
            <el-icon>
              <ele-Refresh/>
            </el-icon>
            重置
          </el-button>
        </el-form-item>
        <el-form-item v-auth="'dict.add'" class="!mr-4">
          <el-button class="ml10" size="default" type="success" @click="onOpenAddDic('add')">
            <el-icon>
              <ele-FolderAdd/>
            </el-icon>
            新增类型
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="state.tableData.loading" :data="state.tableData.data" style="width: 100%">
        <el-table-column label="序号" type="index" width="80"/>
        <el-table-column label="字典名称" prop="dictName" show-overflow-tooltip/>
        <el-table-column label="字典类型" prop="dictType" show-overflow-tooltip>
          <template #default="scope">
            <router-link :to="'/system/dict/data/' + scope.row.id" class="link-type">
              <el-link type="primary">{{ scope.row.dictType }}</el-link>
            </router-link>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" prop="active">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.active"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip/>
        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip/>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button v-auth="'dict.edit'" size="default" text type="primary"
                       @click="onOpenEditDic('edit', scope.row)">
              修改
            </el-button>
            <el-button v-auth="'dict.del'" size="default" text type="primary" @click="onRowDel(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="state.tableData.param.pageNum"
          v-model:page-size="state.tableData.param.pageSize"
          :page-sizes="[10, 20, 30]"
          :pager-count="5"
          :total="state.tableData.total"
          background
          class="mt15"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <DicDialog ref="dicDialogRef" @refresh="getTableData()"/>
  </div>
</template>

<script lang="ts" name="dict" setup>
import {defineAsyncComponent, getCurrentInstance, onMounted, reactive, ref} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import {dictApi} from "/@/api/sys/dict";

// 引入组件
const DicDialog = defineAsyncComponent(() => import('/@/views/system/dict/dialog.vue'));
const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");

// 定义变量内容
const dicDialogRef = ref();
const queryFormRef = ref();
const state = reactive<SysDicState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
      dataObj: {
        dictName: '',
        dictType: ''
      }
    },
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  dictApi()
      .getTableData(state.tableData.param)
      .then(res => {
        state.tableData.data = res.data.records;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
      })
      .catch(() => {
        state.tableData.loading = false;
      })
};

// 打开新增字典弹窗
const onOpenAddDic = (type: string) => {
  dicDialogRef.value.openDialog(type);
};

// 打开修改字典弹窗
const onOpenEditDic = (type: string, row: RowDicType) => {
  dicDialogRef.value.openDialog(type, row);
};

// 删除字典
const onRowDel = (row: RowDicType) => {
  ElMessageBox.confirm(`此操作将永久删除字典名称：“${row.dictName}”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(() => {
        dictApi()
            .delRecord(row)
            .then(() => {
              getTableData();
              ElMessage.success('删除成功');
            })
      })
      .catch(() => {
        ElMessage.info('已取消删除');
      });
};

// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  getTableData();
});

// 重置查询表单
const onHandleResetFields = () => {
  queryFormRef.value.resetFields();
  getTableData();
};
</script>
